package com.hxzy.common.global;

import com.hxzy.common.enums.AckCode;
import com.hxzy.common.exception.HospitalException;
import com.hxzy.common.exception.ServiceException;
import com.hxzy.common.vo.R;
import lombok.extern.log4j.Log4j2;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * springmvc 全局异常处理的类
 *
 * @author tonneyyy
 */
@Log4j2
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = BindException.class)
    public R bindingResult(BindException bindException){
        log.error(bindException);
        List<ObjectError> allErrors = bindException.getAllErrors();
        String msg=allErrors.get(0).getDefaultMessage();
        R r= R.build(AckCode.FAIL);
        r.setMsg(msg);
        return r;
    }

    /**
     * 自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = ServiceException.class)
    public R serviceException(ServiceException e){
        log.error(e);
        R r= R.build(e.getAckCode());
        return r;
    }

    /**
     * 自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = HospitalException.class)
    public R hospitalException(HospitalException e){
        log.error(e);
        R r= R.build(AckCode.FAIL);
        r.setMsg(e.getMessage());
        return r;
    }

    /**
     * 请求方式不支持
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public R handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e,
                                                 HttpServletRequest request)
    {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod());
        R r=R.build(AckCode.FAIL);
        r.setMsg(e.getMessage());
        return r;
    }



    /**
     * 拦截未知的运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public R handleRuntimeException(RuntimeException e, HttpServletRequest request)
    {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',发生未知异常.", requestURI, e);
        R r=R.build(AckCode.FAIL);
        r.setMsg(e.getMessage());
        return r;
    }

    /**
     * 系统异常
     */
    @ExceptionHandler(Exception.class)
    public R handleException(Exception e, HttpServletRequest request)
    {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',发生系统异常.", requestURI, e);
        R r=R.build(AckCode.FAIL);
        r.setMsg(e.getMessage());
        return r;
    }



    /**
     * 自定义验证异常
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e)
    {
        log.error(e.getMessage(), e);
        String message = e.getBindingResult().getFieldError().getDefaultMessage();
        R r=R.build(AckCode.FAIL);
        r.setMsg(message);
        return r;
    }

//    /**
//     * spring security没有权限访问
//     * @param e
//     * @return
//     */
//    @ExceptionHandler(value =AccessDeniedException.class )
//    public R  accessDeniedException(AccessDeniedException  e){
//        log.error(e);
//        R r = R.build(AckCode.PERMISSION_NOT_ACCESS);
//        return r;
//    }

//    /**
//     * spring security没有权限访问
//     * @param e
//     * @return
//     */
//    @ExceptionHandler(value =AccessDeniedException.class )
//    public R  accessDeniedException(AccessDeniedException  e){
//        log.error(e);
//        R r = R.build(AckCode.PERMISSION_NOT_ACCESS);
//        return r;
//    }
//
//    /**
//     * 密码错误
//     * @param badCredentialsException
//     * @return
//     */
//    @ExceptionHandler(value = BadCredentialsException.class)
//    public R passwordError(BadCredentialsException badCredentialsException){
//        log.error(badCredentialsException);
//        R r = R.build(AckCode.LOGIN_ACCOUNT_PASSWORD_ERROR);
//        return r;
//    }
}
